package com.net.common.web;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.common.base.Strings;
import com.google.gson.Gson;
import com.net.common.encryption.AesEncryption;

/**
 * @author  on 2017/6/13.
 * Cookie相关的操作
 */
public class CookieHelper {
    private static final Gson GSON = new Gson();

    public static Cookie createCookie(String path, String name, Object value, int maxAge, String encryptKey) {
        String str = value == null ? "" : GSON.toJson(value);
        if (!Strings.isNullOrEmpty(encryptKey)) {
            str = new AesEncryption(encryptKey).encrypt(str);
        }
        Cookie cookie = new Cookie(name, str);
        cookie.setMaxAge(maxAge);
        cookie.setPath(path);

        return cookie;
    }

    public static void setCookie(HttpServletResponse response, String domain, Cookie cookie) {
        cookie.setDomain(domain);
        response.addCookie(cookie);
    }

    public static <T> T getCookie(HttpServletRequest request, String name, Class<T> clazz, String encryptKey) {
        String cookieValue = getCookie(request, name);

        if (cookieValue == null) {
            return null;
        }

        if (!Strings.isNullOrEmpty(encryptKey)) {
            cookieValue = new AesEncryption(encryptKey).decrypt(cookieValue);
        }

        return toObject(clazz, cookieValue);
    }

    public static String getCookie(HttpServletRequest request, String name) {
        Cookie[] cookies = request.getCookies();
        if (cookies == null || name == null || name.length() == 0) {
            return null;
        }
        for (Cookie cookie : cookies) {
            if (name.equals(cookie.getName())) {
                return cookie.getValue();
            }
        }
        return null;
    }

    public static Cookie deleteCookie(HttpServletResponse response, String domain, String path, String name) {
        Cookie cookie = new Cookie(name, null);
        cookie.setMaxAge(0);
        cookie.setPath(path);
        cookie.setDomain(domain);
        response.addCookie(cookie);
        return cookie;
    }

    @SuppressWarnings("unchecked")
    public static <T> T toObject(Class<T> clazz, String value) {
        if (Boolean.class == clazz || Boolean.TYPE == clazz) {
            return (T) Boolean.valueOf(value);
        } else if (Byte.class == clazz || Byte.TYPE == clazz) {
            return (T) Byte.valueOf(value);
        } else if (Short.class == clazz || Short.TYPE == clazz) {
            return (T) Short.valueOf(value);
        } else if (Integer.class == clazz || Integer.TYPE == clazz) {
            return (T) Integer.valueOf(value);
        } else if (Long.class == clazz || Long.TYPE == clazz) {
            return (T) Long.valueOf(value);
        } else if (Float.class == clazz || Float.TYPE == clazz) {
            return (T) Float.valueOf(value);
        } else if (Double.class == clazz || Double.TYPE == clazz) {
            return (T) Double.valueOf(value);
        } else if (String.class == clazz) {
            return (T) value;
        } else {
            return GSON.fromJson(value, clazz);
        }
    }
}

